package edu.zut.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 单实例： 在内存中，当前类型的对象只有一个
 * 多实例： 在内存中，有多个当前类型的对象
 *
 * @WebServlet ： 以注解的形式简化servlet映射的配置书写（web.xml）
 * name属性：表示的是当前servlet的名称 == servlet-name
 * urlPatterns： 访问当前servlet的url ==  url-pattern   字符串数组   {"/url1","/url2",....} 如果url只映射一个，可以省略{}
 */
@WebServlet(name = "PVServlet", urlPatterns = "/pv")
public class PVServlet extends HttpServlet {

    //针对当前pv请求的计数器
    int count = 0;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        count ++;
        System.out.println("count = " + count);
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>Title</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "当前请求的访问次数：" + count +
                "</body>\n" +
                "</html>");


    }
}
